using System;
using System.Collections.Generic;
using Headsight.BPMN2WF.BPMN.Model;

namespace Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings
{
    /// <summary>
    /// <see cref="IBPMN2WFTransformations"/> implementation offering 
    /// the registration <see cref="IBPMN2WFTransformation"/>s from the outside. 
    /// </summary>
    public class BPMN2WFTransformations : IBPMN2WFTransformations
    {
        private readonly IDictionary<Type, Type> _mappings;

        public BPMN2WFTransformations()
        {
            _mappings = new Dictionary<Type, Type>();
        }

        public Type Default { get; set; }

        public string Name
        {
            get { return "Transformation via Mapping"; }
        }

        public IBPMN2WFTransformation Get(BPMNFlowNode flowNode)
        {
            Type typeOfBpmn2WfTransformation;

            if (_mappings.ContainsKey(flowNode.TElement.GetType()))
            {
                typeOfBpmn2WfTransformation = _mappings[flowNode.TElement.GetType()];
            }
            else
            {
                typeOfBpmn2WfTransformation = Default;
            }

            var result = Activator.CreateInstance(typeOfBpmn2WfTransformation);

            return (IBPMN2WFTransformation) result;
        }

        public void Register(Type tElementType, Type bpmn2WfTransformationType)
        {
            // ^^ not the best API / implementation for now... but works for my current purposes...

            _mappings[tElementType] = bpmn2WfTransformationType;
        }
    }
}